/**
 * 公共数据绑定下拉
 * @author wonderful
 */
var comSelOpt={
		id:"",//要绑定的控件id
		inputObj:null,//要绑定的输入框jquery对象
		name:"",//要提交的表单值字段名称
		width:false,//宽度，如果不定义则为默认
		height:false,//高度，如果不定义就为默认
		isContainParent:true,//多选情况下选择子节点是否包含父节点
		isInitBindData:false,//初始化的时是否初始化已选择并绑定的数据，默认为true。
		isOpen:false,//控制初始化节点时是否展开，默认展开
	    dataUrl:"",
        searchBar:{                  //搜索栏定义
           isOpen:true,                 //是否开启
           title:""                     //标题
         },
		infoType:"UserOrgs",/*绑定的信息类型
		             *类型值-描述:条件参数1,描述:条件参数2
		             *条件参数值多个以','逗号分隔
		             * UserOrgs 当前登录用户所属机构
		             * UserProjects  当前登录用户所属项目
		             * UserSections  当前登录用户所属标段 可选参数：projectIds (项目id字符串，多个id以英文逗号','分隔)
		             * OrgAll  所有机构
		             * RoleAll 所有角色
		             * UserByOrgAndRole 根据用户获取机构与角色
		             * UserOrgStoreHouse 当前登录用户单位所属库房
		             **/
		mulitCheck:true,//是否显示复选框
        isRadio:true,//是否单选模式
		param:{//针对要查询信息的条件参数，具体见infoType注释
			
		},
		event:{
            callBack:function(val,name){//回调函数，val当前控件的值，name当前控件的显示名称

            }
		}
      };

function initSel(originComSelOpt){
    var comSelOpt = jQuery.extend(true, {},originComSelOpt);
	var treeSetting={
			data : {
				simpleData : {
					enable : true
				}
			},
			check: {
				enable: true,
				chkStyle:"checkbox",
				chkboxType: { "Y": "p", "N": "s" }
			},
			callback : {
				//beforeDrag : beforeDrag,
				//beforeEditName : beforeEditName,
				//beforeRemove : beforeRemove,
				//beforeRename : beforeRename,
				//onRemove : onRemove,
				//onRename : onRename,
				//onClick : zTreeOnClick
			}
		};
	
	if(comSelOpt.async){
		
		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
		    alert("得吃");
		};
		
		treeSetting.async=comSel.async;
		if(treeSetting.callback){
			treeSetting.callback["onAsyncSuccess"]=zTreeOnAsyncSuccess;
		}else{
			treeSetting.callback={
					onAsyncSuccess:zTreeOnAsyncSuccess
			};
		}
	}
	
	if(!comSelOpt.mulitCheck){
		 treeSetting.check.enable = false;
	}
	if(comSelOpt.mulitCheck&&comSelOpt.isContainParent){
		treeSetting.check.chkboxType={ "Y": "p", "N": "ps" };
	}else if(comSelOpt.mulitCheck&&!comSelOpt.isContainParent){
		treeSetting.check.chkboxType={ "Y": "", "N": "ps" };
	}
	if(comSelOpt.isRadio){
		treeSetting.callback.onCheck=function zTreeOnCheck(event, treeId, treeNode) {
		    //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
			var treeObj = $.fn.zTree.getZTreeObj(treeId);
			treeObj.checkAllNodes(false);
			treeObj.checkNode(treeNode,true,true);
		};
	}

	var urls={
        "Org":basePath + "/department/listByCondition",
		"OrgAll":basePath+"/department/listTree",
		"RoleAll":basePath+"/role/listTree",
		"UserByOrgAndRole":basePath+"/user/listTree",
		"project":basePath + "/project/list",
        "section":basePath + "/gzmli/section/getSectionByProjectId",
        "procCompactType":basePath + "/gzmli/codeInfo/list?codeInfoType=5",
        "payType":basePath + "/gzmli/codeInfo/list?codeInfoType=6",
        "procCompactStatus":basePath + "/gzmli/codeInfo/list?codeInfoType=7",
        "planType":basePath + "/gzmli/codeInfo/list?codeInfoType=8",
		"UserOrgs":basePath+"/gzmli/user/listUserOrgs",//获取当前登录用户所属所属机构
        "UserProjects":basePath+"/gzmli/user/listUserProjects",//获取当前登录用户所属所属项目
        "UserSections":basePath+"/gzmli/user/listUserSections",//获取当前登录用户所属所属标段
		"UserOrgStoreHouse":basePath+"/gzmli/user/listUserOrgStoreHouse",//获取当前登录用户所属单位的所属库房
        "storeHouse":basePath + "/gzmli/storehouse/list",
		"procCompact":basePath + "/gzmli/proccompact/list"
	};

    var url=urls[comSelOpt.infoType];

    if(comSelOpt.infoType=="Org"){
    	comSelOpt.dataFilter=function(nodes){

		}
	}else if(comSelOpt.infoType=="project"||
              comSelOpt.infoType=="UserProjects" ) {
        comSelOpt.dataFilter=function(nodes){

            $(nodes).each(function(i,obj){
                obj.id=obj.projectId;
                obj.name=obj.projectName;
            });

        }
	}else if(comSelOpt.infoType=="section"||
              comSelOpt.infoType=="UserSections") {
        comSelOpt.dataFilter=function(nodes){

            $(nodes).each(function(i,obj){
                obj.id=obj.sectionId;
                obj.name=obj.sectionName;
            });

        }
    }
    else if(comSelOpt.infoType=="procCompactType" ||
        comSelOpt.infoType=="payType" ||
        comSelOpt.infoType=="procCompactStatus" ||
        comSelOpt.infoType=="planType"
    ) {
        comSelOpt.dataFilter=function(nodes){

            $(nodes).each(function(i,obj){
                obj.id=obj.codeMarkType;
                obj.name=obj.codeMarkName;
            });

        }
    }else if(comSelOpt.infoType=="UserOrgs") {
        comSelOpt.dataFilter=function(nodes){
            $(nodes).each(function(i,obj){
                obj.id=obj.orgId;
                obj.name=obj.orgName;
            });

        }
    }else if(comSelOpt.infoType == "UserOrgStoreHouse"||
	          comSelOpt.infoType == "storeHouse"){
        comSelOpt.dataFilter=function(nodes){
            $(nodes).each(function(i,obj){
                obj.id=obj.storeHouseId;
                obj.name=obj.storeHouseName;
            });

        }
	}else if (comSelOpt.infoType == "procCompact"){
        comSelOpt.dataFilter=function(nodes){
            $(nodes).each(function(i,obj){
                obj.id=obj.procCompactId;
                obj.name=obj.procCompactName;
            });

        }
	}


    if(comSelOpt.isFixedHeight==null||comSelOpt.isFixedHeight==undefined)
    	comSelOpt.isFixedHeight=true;

	var selOpt={
			id:comSelOpt.id,                     //渲染对象ID
			className:"wdfSel",                //样式名称
			zTreeOption:treeSetting,     //zTree配置参数
			mulitValsChecked:comSelOpt.mulitCheck==undefined?false:comSelOpt.mulitCheck,//是否为多值选择模式
			height:comSelOpt.height?comSelOpt.height:230,                 //下拉面板高度
			width:comSelOpt.width?comSelOpt.width:false,//下拉面板宽度
			isOpen:comSelOpt.isOpen==false?false:true,//初始化节点是否默认展开
			isFixedHeight:comSelOpt.isFixedHeight,//固定高度
			searchBar:comSelOpt.searchBar,//查询栏
			data:comSelOpt.data?comSelOpt.data:undefined,//本地数据
		    root:comSelOpt.root?comSelOpt.root:"data",//设置根属性
			zIndex:9999,                //下拉面板的层级
			/* dataModel:{               //后端对象到下拉树节点对象字段映射
				val:"id",//值字段
				name:"name"//显示字段
			}, */
			valInput:{                   //控件值域的名称和ID用于表单的值提交
				id:comSelOpt.id+"_InputVal",
				name:comSelOpt.name
			},
			treeDataUrl:!comSelOpt.dataUrl?url:comSelOpt.dataUrl, //非异步模式的树控件数据获取地
			param:comSelOpt.param,//要提交的参数
		    dataFilter:comSelOpt.dataFilter,
		    event:comSelOpt.event
			/*event:{
				//输入框值改变后执行
				onChange:function(inputObj,event){
			    //alert(inputObj.val());
				},
				//树数据绑定后执行
				onZTreeLoaded:function(zTree,selObj){
					 //$("#testWdfSel").wdfSel("setVal","C6A87E584360000140F3680016E01DB0,C6A87E9028D00001B6D61CB9613D1070,C6A88103E9800001979122602D3AE5A0,C6A88105DE900001121D2120435091C0");
					comSelOpt.callBack(selObj.data("inputVal").val(),selObj.val());
				}	
			}*/
	  };
	
	if(comSelOpt.dataModel){
		selOpt.dataModel=comSelOpt.dataModel;
	}
	
    if(!comSelOpt.isInitBindData&&comSelOpt.isInitBindData==false){
    	selOpt.isInitBindData=false;
    }	
	
	if(comSelOpt.inputObj){
        if(comSelOpt.isInitBindData)
		   comSelOpt.inputObj.val("");
		   comSelOpt.inputObj.wdfSel("init",selOpt);
	}else{
        if (comSelOpt.isInitBindData)
            $("#" + selOpt.id).val("");
            $("#" + selOpt.id).wdfSel("init", selOpt);
	}
} 